package com.tt.fashionWebSite.controller;

import com.tt.fashionWebSite.domain.LoaiDanhMucFull;
import com.tt.fashionWebSite.dto.*;
import com.tt.fashionWebSite.service.ProductService;
import com.tt.fashionWebSite.service.WomenService;
import com.tt.fashionWebSite.util.ISSConstants;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * Created by isoft-work 5 on 12/7/2014.
 */
@Controller
public class ProductController {
   @Autowired
   private ProductService productService;

   @Autowired
   private WomenService womenService;

   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/product/toPageProduct/{Gender}/{Id}", method = RequestMethod.GET)
   public @ResponseBody
   ModelAndView toPageProduct(HttpServletRequest request,@PathVariable String Gender, @PathVariable String Id, Map<String, Object> map) throws ParseException {
      map.put("maDMSP", Id);
      return new ModelAndView("user/user-women-product");
   }

   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/product/getProductByIdDMSP/{Gender}/{Id}", method = RequestMethod.GET)
   public @ResponseBody
   JsonResponseDto<List<SanPhamFullDto>> getProductByIdDMSP(HttpServletRequest request, @PathVariable String Gender, @PathVariable String Id) throws ParseException {
      return new JsonResponseDto<List<SanPhamFullDto>>(productService.getProductByIdDMSP(Gender, Id), JsonResponseCode.SUCCESS);
   }

   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/product/getCategoryMenu/{idLoaiDM}/{gender}", method = RequestMethod.GET)
   public @ResponseBody
   JsonResponseDto<LoaiDanhMucFullDto> getListCatalog(HttpServletRequest request, @PathVariable String idLoaiDM, @PathVariable String gender) {
      return new JsonResponseDto<LoaiDanhMucFullDto>(womenService.getListMenuByIdDM(idLoaiDM, gender), JsonResponseCode.SUCCESS);
   }

   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/product/toPageProductDetail/{IdSP}", method = RequestMethod.GET)
   public @ResponseBody
   ModelAndView toPageProductDetail(HttpServletRequest request, @PathVariable String IdSP, Map<String, Object> map) throws ParseException {
      map.put("maSP", IdSP);
      return new ModelAndView("user/user-product-detail");
   }

   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/product/getProductDetail/{IdSP}", method = RequestMethod.GET)
   public @ResponseBody
   JsonResponseDto<SanPhamFullDto> getProductDetail(HttpServletRequest request, @PathVariable String IdSP) throws ParseException {
      return new JsonResponseDto<SanPhamFullDto>(productService.getProduct(IdSP), JsonResponseCode.SUCCESS);
   }

   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/product/getProductFromRule/{IdSP}", method = RequestMethod.GET)
   public @ResponseBody
   JsonResponseDto<List<SanPhamFullDto>> getProductFromRule(HttpServletRequest request, @PathVariable String IdSP) throws ParseException {
      HttpSession s = request.getSession();
      GioHangDto gioHang = (GioHangDto)s.getAttribute("shopCart");
      return new JsonResponseDto<List<SanPhamFullDto>>(productService.getProductFromRule(IdSP, gioHang), JsonResponseCode.SUCCESS);
   }

   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/product/addCart", method = RequestMethod.POST)
   public @ResponseBody
   JsonResponseDto<GioHangDto> addCart(HttpServletRequest request, @RequestBody ChiTietSanPhamFullDto chiTietSanPhamFull) throws ParseException {
      HttpSession s = request.getSession();
      GioHangDto gioHangOut = (GioHangDto)s.getAttribute("shopCart");
      GioHangDto gioHangNew = productService.addCart(chiTietSanPhamFull, gioHangOut);
      if(gioHangNew == null) {
         s.setAttribute("shopCart", gioHangOut);
         return new JsonResponseDto<GioHangDto>(gioHangOut, JsonResponseCode.FAILED);
      }
      else {
         s.setAttribute("shopCart", gioHangNew);
         return new JsonResponseDto<GioHangDto>(gioHangNew, JsonResponseCode.SUCCESS);
      }
   }

   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/product/getListCart", method = RequestMethod.GET)
   public @ResponseBody
   JsonResponseDto<GioHangDto> getListCart(HttpServletRequest request) throws ParseException {
      HttpSession s = request.getSession();
      GioHangDto gioHang = (GioHangDto)s.getAttribute("shopCart");
      return new JsonResponseDto<GioHangDto>(gioHang, JsonResponseCode.SUCCESS);
   }

   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/product/updateCart/{id}/{num}", method = RequestMethod.GET)
   public @ResponseBody
   JsonResponseDto<GioHangDto> updateCart(HttpServletRequest request, @PathVariable String id, @PathVariable Integer num) throws ParseException {
      HttpSession s = request.getSession();
      GioHangDto gioHang = (GioHangDto)s.getAttribute("shopCart");
      GioHangDto gioHangNew = productService.updateCart(id, num, gioHang);
      if(gioHangNew == null) {
         s.setAttribute("shopCart", gioHang);
         return new JsonResponseDto<GioHangDto>(gioHang, JsonResponseCode.FAILED);
      }
      else {
         s.setAttribute("shopCart", gioHangNew);
         return new JsonResponseDto<GioHangDto>(gioHangNew, JsonResponseCode.SUCCESS);
      }

   }

   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/product/deleteCart/{id}", method = RequestMethod.GET)
   public @ResponseBody
   JsonResponseDto<GioHangDto> deleteCart(HttpServletRequest request, @PathVariable String id) throws ParseException {
      HttpSession s = request.getSession();
      GioHangDto gioHang = (GioHangDto)s.getAttribute("shopCart");
      GioHangDto gioHangNew = productService.deleteCart(id, gioHang);
      return new JsonResponseDto<GioHangDto>(gioHang, JsonResponseCode.SUCCESS);
   }

   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/product/search/", method = RequestMethod.POST)
   public @ResponseBody
   void searchProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      request.setCharacterEncoding("UTF-8");
      String name = request.getParameter("value_search");
      request.setAttribute("name", name);
      try {
         RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/user/user-search-product.jsp");
         dispatcher.forward(request, response);
      } catch (ServletException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/product/search/getProduct/{name}", method = RequestMethod.GET)
   public @ResponseBody
   JsonResponseDto<List<SanPhamFullDto>> getProductSearch(HttpServletRequest request, @PathVariable String name) throws ServletException, IOException {
      request.setCharacterEncoding("UTF-8");
      return new JsonResponseDto<List<SanPhamFullDto>>(productService.getProductSearch(name), JsonResponseCode.SUCCESS);
   }
}
